package com.originskyseed.platform.cms.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.ComplainAndSuggest;
import com.originskyseed.platform.cms.model.ComplainAndSuggestRelReply;
import com.originskyseed.platform.cms.service.IComplainAndSuggestRelReplyService;
import com.originskyseed.platform.cms.service.IComplainAndSuggestService;
import com.originskyseed.platform.cms.vo.request.ComplainAndSuggestRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 投诉与建议表Controller类（OSI_COMPLAIN_AND_SUGGEST）
 * 
 * @author WuZhuoXi
 */
@Controller
@RequestMapping("/complainAndSuggest")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class ComplainAndSuggestController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ComplainAndSuggestController.class);

	@DubboReference
	private IComplainAndSuggestService complainAndSuggestService;

	@DubboReference
	private IComplainAndSuggestRelReplyService complainAndSuggestRelReplyService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainAndSuggestList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<ComplainAndSuggest>> complainAndSuggestList(HttpServletRequest request) {
		BaseResponseBody<BasePage<ComplainAndSuggest>> result = null;
		try {
			// 请求对象
			ComplainAndSuggestRequestVO requestVO = this.getParameterBean(ComplainAndSuggestRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 分页查询
			BasePage<ComplainAndSuggest> page = new BasePage<ComplainAndSuggest>(requestVO.getPageNo(), requestVO.getPageSize());
			if (userInfo != null && (userInfo.getSourceSys() == 0))// 如果是推广总裁或是内容总裁，可以看到所有信息
			{
				page = this.complainAndSuggestService.selectListByForwardIdByPage(requestVO.toMap(), page);
			} else {
				requestVO.setForwardId(userInfo.getId());
				requestVO.setCreatorId(userInfo.getId());
				page = this.complainAndSuggestService.selectListByForwardIdByPage(requestVO.toMap(), page);
			}
			if (page != null && page.getResults() != null) {
				List<ComplainAndSuggest> complainAndSuggestList = page.getResults();
				if (complainAndSuggestList != null && complainAndSuggestList.size() > 0) {
					for (int i = 0; i < complainAndSuggestList.size(); i++) {
						ComplainAndSuggest complainAndSuggest = complainAndSuggestList.get(i);

						if (userInfo.getSourceSys() == 0) {
							complainAndSuggest.setIsDisReply(1);
							complainAndSuggest.setIsDisForward(1);
						} else {
							if (complainAndSuggest.getForwardId()!=null&&complainAndSuggest.getForwardId().equals(userInfo.getId())) {
								complainAndSuggest.setIsDisReply(1);
							} else {
								complainAndSuggest.setIsDisReply(0);
							}
							complainAndSuggest.setIsDisForward(0);
						}
					}
				}
			}

			result = new BaseResponseBody<BasePage<ComplainAndSuggest>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<ComplainAndSuggest>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainAndSuggestSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<ComplainAndSuggest> complainAndSuggestSave(HttpServletRequest request) {
		BaseResponseBody<ComplainAndSuggest> result = null;
		try {
			// 请求对象
			ComplainAndSuggest complainAndSuggest = this.getParameterBean(ComplainAndSuggest.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			if (StringUtils.isNotBlank(complainAndSuggest.getId())) {
				this.complainAndSuggestService.updateByPrimaryKeySelective(complainAndSuggest, userInfo);
			} else {
				complainAndSuggest.setState(2);
				complainAndSuggest.setIsRead(0);
				complainAndSuggest.setCreatorId(userInfo.getId());
				complainAndSuggest.setCreator(userInfo.getName());
				this.complainAndSuggestService.insertSelective(complainAndSuggest);
			}
			result = new BaseResponseBody<ComplainAndSuggest>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_SUCCESS.getMsg()), complainAndSuggest);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<ComplainAndSuggest>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainAndSuggestDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> complainAndSuggestDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.complainAndSuggestService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainAndSuggestDetailById.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody<ComplainAndSuggest> giftDetails(HttpServletRequest request) {
		BaseResponseBody<ComplainAndSuggest> result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);
			ComplainAndSuggest complainAndSuggest = this.complainAndSuggestService.selectByPrimaryKey(id);
			if(complainAndSuggest!=null) {
				List<ComplainAndSuggestRelReply> complainAndSuggestRelReplyList = this.complainAndSuggestRelReplyService.selectListBySuggestId(id);
				complainAndSuggest.setComplainAndSuggestRelReplyList(complainAndSuggestRelReplyList);
			}
			result = new BaseResponseBody<ComplainAndSuggest>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), complainAndSuggest);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<ComplainAndSuggest>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}